/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.cspimp.psp.service.impl;

import me.zhengjie.modules.cspimp.psp.domain.ChemiclaScrapDisposition;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.psp.repository.ChemiclaScrapDispositionRepository;
import me.zhengjie.modules.cspimp.psp.service.ChemiclaScrapDispositionService;
import me.zhengjie.modules.cspimp.psp.service.dto.ChemiclaScrapDispositionDto;
import me.zhengjie.modules.cspimp.psp.service.dto.ChemiclaScrapDispositionQueryCriteria;
import me.zhengjie.modules.cspimp.psp.service.mapstruct.ChemiclaScrapDispositionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author zmm
 * @website https://docs.auauz.net
 * @description 服务实现
 * @date 2020-05-30
 **/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ChemiclaScrapDispositionServiceImpl implements ChemiclaScrapDispositionService {

    private final ChemiclaScrapDispositionRepository chemiclaScrapDispositionRepository;
    private final ChemiclaScrapDispositionMapper chemiclaScrapDispositionMapper;
    private final UserRepository userRepository;

    @Override
    public Map<String, Object> queryAll(ChemiclaScrapDispositionQueryCriteria criteria, Pageable pageable) {
        Page<ChemiclaScrapDisposition> page = chemiclaScrapDispositionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(chemiclaScrapDispositionMapper::toDto));
    }

    @Override
    public List<ChemiclaScrapDispositionDto> queryAll(ChemiclaScrapDispositionQueryCriteria criteria) {
        return chemiclaScrapDispositionMapper.toDto(chemiclaScrapDispositionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public ChemiclaScrapDispositionDto findById(Long id) {
        ChemiclaScrapDisposition chemiclaScrapDisposition = chemiclaScrapDispositionRepository.findById(id).orElseGet(ChemiclaScrapDisposition::new);
        ValidationUtil.isNull(chemiclaScrapDisposition.getId(), "ChemiclaScrapDisposition", "id", id);
        User user = userRepository.findById(chemiclaScrapDisposition.getAuditId()).get();
        ChemiclaScrapDispositionDto chemiclaScrapDispositionDto = new ChemiclaScrapDispositionDto();
        BeanUtils.copyProperties(chemiclaScrapDisposition, chemiclaScrapDispositionDto);
        chemiclaScrapDispositionDto.setUsername(user.getUsername());
        return chemiclaScrapDispositionMapper.toDto(chemiclaScrapDisposition);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ChemiclaScrapDispositionDto create(ChemiclaScrapDisposition resources) {
        resources.setCid(0l);
        return chemiclaScrapDispositionMapper.toDto(chemiclaScrapDispositionRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ChemiclaScrapDisposition resources) {
        ChemiclaScrapDisposition chemiclaScrapDisposition = chemiclaScrapDispositionRepository.findById(resources.getId()).orElseGet(ChemiclaScrapDisposition::new);
        ValidationUtil.isNull(chemiclaScrapDisposition.getId(), "ChemiclaScrapDisposition", "id", resources.getId());
        chemiclaScrapDisposition.copy(resources);
        chemiclaScrapDispositionRepository.save(chemiclaScrapDisposition);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            chemiclaScrapDispositionRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ChemiclaScrapDispositionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ChemiclaScrapDispositionDto chemiclaScrapDisposition : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("创建者", chemiclaScrapDisposition.getCreateBy());
            map.put("更新者", chemiclaScrapDisposition.getUpdateBy());
            map.put("创建日期", chemiclaScrapDisposition.getCreateTime());
            map.put("更新时间", chemiclaScrapDisposition.getUpdateTime());
            map.put("报废量", chemiclaScrapDisposition.getScrapCount());
            map.put("审核人", chemiclaScrapDisposition.getAuditId());
            map.put("化学品id", chemiclaScrapDisposition.getChemiclaId());
            map.put("当前库存量", chemiclaScrapDisposition.getCurrentStockCount());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}